package com.yuzhitong.shapi.presenter;

import com.alibaba.fastjson.JSON;
import a.a.a.MyApplication;
import com.yuzhitong.shapi.base.BasePresenter;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.base.net.BaseListBean;
import com.yuzhitong.shapi.bean.CategoryBean;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.contract.CategoryContract;
import com.yuzhitong.shapi.model.CategoryModel;
import com.yuzhitong.shapi.net.MyObserver;
import com.yuzhitong.shapi.net.RxScheduler;

import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by Brsya
 * CreateDate: 2022/5/8  20:06
 * Description:
 * 分类筛选页
 */
public class CategoryPresenter extends BasePresenter<CategoryContract.View> implements CategoryContract.Presenter {
    private CategoryContract.Model model;

    public CategoryPresenter() {
        model = new CategoryModel();
    }

    @Override
    public void getMovieList(int pageNumber, String type, String tags, String area, String year, String sort) {
        mView.showLoading();

        Map<String, Object> par = getPublicPar();
        if (type != null) {
            par.put("type", type);
        }
        if (tags != null) {
            par.put("tags", tags);
        }
        if (area != null) {
            par.put("area", area);
        }
        if (year != null) {
            par.put("year", year);
        }
        if (sort != null) {
            par.put("sort", sort);
        }
        par.put("page", pageNumber);
        par.put("size", Contents.PAGE_SIZE);

        model.getMovieList(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<MovieBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<MovieBean> baseBean) {
                        baseBean.handleData(MovieBean.class);
                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                            mView.showMovieList(baseBean.getData());
                        }
                        if (baseBean.isNoMore()) {
                            mView.showNoMore();
                        }
                    }
                });
    }

    @Override
    public void getCategory() {
        String category = MyApplication.aCache.getAsString(Contents.CACHE_KEY_CATEGORY);
        List<CategoryBean> categoryBeans = JSON.parseArray(category, CategoryBean.class);
        mView.showCategory(categoryBeans);


//        mView.showLoading();
        //        model.getCategory(getPublicPar())
//                .compose(RxScheduler.Obs_io_main())
//                .to(mView.bindAutoDispose())
//                .subscribe(new MyObserver<BaseListBean<CategoryBean>>(mView) {
//                    @Override
//                    public void success(@NonNull BaseListBean<CategoryBean> baseBean) {
//                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
//                            mView.showCategory(baseBean.getData());
//                        }
//                    }
//                });
    }
}
